package dom.xpath.query.xpath.expr;

import dom.xpath.query.EvalException;
import dom.xpath.query.xpath.XPContext;
import dom.xpath.query.xpath.locpath.LocationPath;
import dom.xpath.query.xpath.values.Bool;
import dom.xpath.query.xpath.values.Comp;
import dom.xpath.query.xpath.values.Item;

/**
 * Abstract Compare Expression.
 * 
 * @author Workgroup DBIS, University of Konstanz 2005-07, ISC License
 * @author Christian Gruen
 */
public abstract class Comparison extends DualExpr {
  /** Expression Type. */
  public Comp type;

  @Override
  public Bool eval(final XPContext ctx) throws EvalException {
    final Item v1 = ctx.eval(expr1);
    final Item v2 = ctx.eval(expr2);
    // don't evaluate empty node sets
    return Bool.get(v1.size() != 0 && v2.size() != 0 && type.eval(v1, v2));
  }
  
  /**
   * Checks if this equality expression is a simple one (LocationPath +
   * XPathValue and equals check),
   * accessing the indexes.
   * @return result of check
   */
  public final boolean simple() {
    return expr1 instanceof LocationPath && expr2 instanceof Item;
  }
  
  @Override
  public final String toString() {
    return expr1 + " " + type + " " + expr2;
  }
}
